﻿namespace ECommerce.Controls
{
	using System;
	using System.Linq;
	using System.Web.UI.WebControls;

	public partial class RecommendProducts9_15 : BaseUserControl
	{
		private Service.IProductService _ProductService;

		protected void Page_PreRender(object sender, EventArgs e)
		{
			BindProducts();
		}

		protected void RptrRecommendProducts9_15_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.DataItem != null)
			{
				var HLnkProd = e.Item.FindControl("HLnkProd") as HyperLink;
				var prd = e.Item.DataItem as Data.Entity.Product;

				HLnkProd.ImageUrl = "~/PictureHandler.ashx?picturesize=85&productid=" + prd.ProductID.ToString();
				HLnkProd.NavigateUrl = "~/ProductDetail.aspx?productid=" + prd.ProductID.ToString();
				HLnkProd.ToolTip = prd.Name;
			}
		}

		private void BindProducts()
		{
			if (SiteContext.Current == null || SiteContext.Current.WorkingLanguage == null)
			{
				Server.Transfer("~/HomePage.aspx");
			}

			if (_ProductService == null)
			{
				_ProductService = new Service.ProductService();
			}

			var productsByLang = _ProductService.GetProductsByLanguage(SiteContext.Current.WorkingLanguage.LanguageId);

			if (productsByLang != null)
			{
				var products = (from p in productsByLang
								where p.Recommend.GetValueOrDefault()
								orderby p.DisplayOrder
								select p).Skip(8).Take(7);

				RptrRecommendProducts9_15.DataSource = products;
				RptrRecommendProducts9_15.DataBind();
			}
			else
			{
				RptrRecommendProducts9_15.DataSource = null;
				RptrRecommendProducts9_15.DataBind();
			}
		}
	}
}